﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

public partial class UserControl_ControlCustomer : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
                if (ViewState["flag"] == null)
                {
                    ViewState["flag"] = false;
                }
           
            BindTinhThanh();

            BindQuanHuyen(ddlTinhThanh.SelectedValue.ToString());

            //nằm trong thread
            ThreadStart thrstar = new ThreadStart(BindGender);
            Thread tr = new Thread(thrstar);
            tr.Start();
            //năm trong thread
        }
    }
    protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        txtMaSo.Text = "New";
        txtDiaChiLienLac.Text = "";
        txtDiaChiThuongTru.Text = "";
        txtHoTen.Text = "";
        txtMemoryDate.Text = "";
        txtMobile.Text = "";
        txtNgaySinh.Text = "";
        txtNgheNghiep.Text = "";
        txtSoCMND.Text = "";
        ViewState["flag"] = true;
    }
    protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        int i=0;
        try
        {
            if ((bool)ViewState["flag"])
            {
                //insert
                dt = new ClassExecute.ClassExecute();
                i = dt.ExecuteSQl_int(str, "k_insertArcust", CommandType.StoredProcedure, ref error,
                    new SqlParameter("@customer_name", txtHoTen.Text),
                    new SqlParameter("@address", txtDiaChiLienLac.Text),
                    new SqlParameter("@IDCardNo", txtSoCMND.Text),
                    new SqlParameter("@Birthday", txtNgaySinh.Text),
                    new SqlParameter("@Memorydate", txtMemoryDate.Text),
                    new SqlParameter("@Gender", ddlXungHo.SelectedValue.ToString()),
                    new SqlParameter("@ThuongTru", txtDiaChiThuongTru.Text),
                    new SqlParameter("@ProvinceCode", ddlTinhThanh.SelectedValue.ToString()),
                    new SqlParameter("@TownCode", ddlQuanHuyen.SelectedValue.ToString()),
                    new SqlParameter("@Career", txtNgheNghiep.Text),
                    new SqlParameter("@phone", txtMobile.Text));
                if (i == 1)
                {
                    uscMsgBox1.AddMessage("Thêm mới khách hàng thành công", uscMsgBox.enmMessageType.Success);
                    ViewState["flag"] = false;

                    txtMaSo.Text = "";
                    txtDiaChiLienLac.Text = "";
                    txtDiaChiThuongTru.Text = "";
                    txtHoTen.Text = "";
                    txtMemoryDate.Text = "";
                    txtMobile.Text = "";
                    txtNgaySinh.Text = "";
                    txtNgheNghiep.Text = "";
                    txtSoCMND.Text = "";

                }
                else
                {
                    uscMsgBox1.AddMessage(DRMConstants.MESSAGE_UNDER_CONSTRUCTION, uscMsgBox.enmMessageType.Error);
                }
            }
            else
            {
                //update
                dt = new ClassExecute.ClassExecute();
                i = dt.ExecuteSQl_int(str, "k_insertArcust", CommandType.StoredProcedure, ref error,
                    new SqlParameter("@customer_name", txtHoTen.Text),
                    new SqlParameter("@address", txtDiaChiLienLac.Text),
                    new SqlParameter("@IDCardNo", txtSoCMND.Text),
                    new SqlParameter("@Birthday", txtNgaySinh.Text),
                    new SqlParameter("@Memorydate", txtMemoryDate.Text),
                    new SqlParameter("@Gender", ddlXungHo.SelectedItem.Text),
                    new SqlParameter("@ThuongTru", txtDiaChiThuongTru.Text),
                    new SqlParameter("@ProvinceCode", ddlTinhThanh.SelectedValue.ToString()),
                    new SqlParameter("@TownCode", ddlQuanHuyen.SelectedValue.ToString()),
                    new SqlParameter("@Career", txtNgheNghiep.Text),
                    new SqlParameter("@phone", txtMobile.Text),
                    new SqlParameter("@customer_key",int.Parse(txtMaSo.Text)));
                if (i == 1)
                {
                    uscMsgBox1.AddMessage("Cập nhật khách hàng thành công", uscMsgBox.enmMessageType.Success);
                }
                else
                {
                    uscMsgBox1.AddMessage("Đề nghị cung cấp mã khách hàng để cập nhật", uscMsgBox.enmMessageType.Error);
                }

            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }   
    }
    protected void imgBtnDelete_Click(object sender, ImageClickEventArgs e)
    {

    }
    protected void imgBtnFind_Click(object sender, ImageClickEventArgs e)
    {
        TabContainer1.ActiveTabIndex = 1;
    }
    protected void btSearch_Click(object sender, EventArgs e)
    {
        LoadGrid(); 
    }
    protected void grvArcust_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvArcust.PageIndex = e.NewPageIndex;
        LoadGrid();
    }
    protected void ddlTinhThanh_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindQuanHuyen(ddlTinhThanh.SelectedValue.ToString());
    }
    void BindTinhThanh()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db=dt.Get_Data_DataTable(str,ref error,"k_getTinhThanh",CommandType.StoredProcedure);
        dt.BindDropDownList(db, ddlTinhThanh, "MaTinh", "TinhThanh");
    }
    void BindQuanHuyen(string MaTinh)
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getQuanHuyen", CommandType.StoredProcedure,
            new SqlParameter("@MaTinh",MaTinh));
        dt.BindDropDownList(db, ddlQuanHuyen, "MaQuan", "QuanHuyen");
    }
    void LoadGrid()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_searchArcust", CommandType.StoredProcedure,
            new SqlParameter("@para", txtSearch.Text));
        grvArcust.DataSource = db;
        grvArcust.DataBind();
        if (db.Rows.Count == 0)
        {
            uscMsgBox1.AddMessage("Không tìm thấy kết quả", uscMsgBox.enmMessageType.Attention);
        }
    }
    protected void grvArcust_RowCommand(object sender, GridViewCommandEventArgs e)
    {
         dt=new ClassExecute.ClassExecute();
         try
         {
             if (e.CommandName == "chon")
             {

                 GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
                 Label lblMaSo = (Label)grv.FindControl("lblMaSo");

                 DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getDetailArcust", CommandType.StoredProcedure,
                     new SqlParameter("@customer_key", lblMaSo.Text));

                 txtMaSo.Text = db.Rows[0]["customer_key"].ToString();
                 txtHoTen.Text = db.Rows[0]["customer_name"].ToString(); 
                 txtDiaChiLienLac.Text=db.Rows[0]["address"].ToString(); 
                 txtSoCMND.Text=db.Rows[0]["IDCardNo"].ToString(); 
                 txtNgaySinh.Text=db.Rows[0]["Birthday"].ToString(); 
                 txtMemoryDate.Text=db.Rows[0]["Memorydate"].ToString(); 
                 try
                 {
                     ddlXungHo.SelectedValue=db.Rows[0]["Gender"].ToString(); 
                 }
                 catch
                 {

                 }
                 txtDiaChiThuongTru.Text=db.Rows[0]["ThuongTru"].ToString(); 
                  try
                 {
                      
                     ddlTinhThanh.SelectedValue=db.Rows[0]["ProvinceCode"].ToString(); 
                 }
                 catch
                 {

                 }
                  try
                 {
                      
                     ddlQuanHuyen.SelectedValue=db.Rows[0]["TownCode"].ToString(); 
                 }
                 catch
                 {

                 }
                 txtNgheNghiep.Text=db.Rows[0]["Career"].ToString(); 
                 txtMobile.Text=db.Rows[0]["phone"].ToString();

                 grvArcust.DataSource = null;
                 grvArcust.DataBind();

                 ViewState["flag"] = false;
                 TabContainer1.ActiveTabIndex = 0;
             }
         }
         catch (Exception ex)
         {

             string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
             dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
         }   
    }
    void BindGender()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "k_getListGender", CommandType.StoredProcedure);
        dt.BindDropDownList(db, ddlXungHo, "GenderID", "GenderName");
    }
}